 
 cd "/disk/agedisk3/medicare.work/chandra-DUA52080/pragya-dua52080/replication/"

** FILE DESCRIPTION: This file attempts to repeat the dynamic decomposition using the following outcomes: 
** beta blockers (discharge ) and aspirin (discharge)

** BEFORE RUNNING: 
** STEP 1: Change the global CCP_file to refer to the path for the CCP data & re-reference the working directory

** Notes
** NOTE 2: We do not risk-adjust because the CMS measures are not risk-adjusted
** NOTE 3: We limit to the set of hospitals in the main sample that also exist in both datasets (CCP, CMS)

************************************************************************************************************************************
************************************************************************************************************************************
** SET UP***************************************************************************************************************************
************************************************************************************************************************************
************************************************************************************************************************************

version 14.0
cd " Programs"
** This is a placeholder file path for the raw CCP data; this data cannot be shared and actually lives at Dartmouth. 
global CCP =  " CCP_data.dta"	

** Makes the data
global MAKEDATA = 1

** Generates bootstrap sample
global BOOTSAMP = 1 

** Generates average of poc measures across patients in each period
global AVERAGES = 1

** Does static decomposition
global STATIC = 1

** Does dynamic decomposition
global DYNAMIC = 1

** List of measure names
local measlist "betablock_discharge aspirin_discharge"

** List of market share types (actual, weighted zip code, weighted HRR)
local sharetypes "act wgt wgtH"

** List of reweight share types (weighted zip code, weighted HRR)
local sharetypes_reweight "wgt wgtH"

** List of race groups
local racegroups "b w"

** Number of bootsamples
** Set this to 0 if you don't want to regenerate standard errors and p-values
global NBOOT = 1000

************************************************************************************************************************************
************************************************************************************************************************************
** CODE START **********************************************************************************************************************
************************************************************************************************************************************
************************************************************************************************************************************

********************** MAKE MASTER DATA SET ************************

if $MAKEDATA == 1{
	
	** STEP 0: Replaces censored patient counts with averages 
		
		** Opens files with hospital specific counts and merges in total counts
		clear
		use  Exportable_IntermediateData/hospitals_black_white_patcount.dta, replace
		generate id = 1
		merge m:1 id using  Exportable_IntermediateData/hospital_totalcounts.dta, assert(match) nogenerate
		drop id
		
		** generates a global indicator for whether hospital has censored white or black market share
		foreach period of numlist 1 4{
		foreach race in `racegroups'{	
			gen byte missing_`race'`period' = npatients_`race'_ami_tot`period' == . 
		}
		}
		
		** Replaced missing patient counts with censored patient counts 
		foreach period of numlist 1 4{
		foreach race in `racegroups'{	
		
			count if missing_`race'`period' == 1
			local hosp_count = r(N)
			replace npatients_`race'_ami_tot`period' = snpatients_`race'_ami_tot_cen`period' / `hosp_count' if missing_`race'`period' == 1 
			
			}
		}
				
		foreach period of numlist 1 4{
		foreach share in `sharetypes_reweight'{
		
			assert npatients_w_ami_tot_`share'`period'	== . if missing_w`period' == 1
			count if missing_w`period' == 1 
			local hosp_count = r(N)
			replace npatients_w_ami_tot_`share'`period' = snpatients_w_ami_tot_`share'_cen`period' / `hosp_count' if missing_w`period' == 1 

			
		}
		}
		
		keep pn_new npat* 
		save  Exportable_IntermediateData/hospitals_market_share.dta, replace 
	
	
	** STEP 1: Generate a hospital-level file for 1995-1999 with outcome measures (unriskadjusted)
	
		** Cleans file 
		clear
		use $CCP, replace
		rename provider pn
		generate discharge_year = year(disdate)
		log using CCP_logfile.log, replace
		display "count of patients per year"
		tab discharge_year
		display "count of unique providers"
		codebook pn, compact
		foreach year of numlist 1994/1999{
			count if discharge_year == `year’ 
			if r(N) != 0 {
		display "count of unique providers in `year’"
		codebook pn if discharge_year == `year’, compact
		}
		}
		log close


		keep if discharge_year > 1993 & discharge_year < 2000
		merge m:1 pn using  Intermediate_Output_Not_Exportable/pn2pn_new.dta, keep(master match) nogenerate
		
		
			** Attempting to create an indicator for LOS > 120 days
			generate los_exc = los_cnt > 120
			
			** Attempting to create an indicator for leaving against medical advice (ever)
			generate med_advice_exc = dstntncd == 7
			
			** Attempting to create an indicator for having a terminal illness
			gen byte term_illness = has_term_illness == 1
			
			** Attempting to create an indicator for dying on day of or day after arrival 
			gen byte died_within1day = dead1d == 1
			
			** Attempting to create an indicator for leaving against medical advice on day of or day after arrival
			gen byte med_advice_within1day_exc = dstntncd ==  7 & los_cnt < 3
			
			** Attempting to create an idicator for being discharged on day of arrival
			gen byte discharged_day_arrival = los_cnt < 2 
					
		******************************************************************************************************
		******************************************************************************************************
		******************************************************************************************************
		
			** Attempting to create a joint indicator for meeting any of the following criteria
			** 1) Patients who died during stay
			** 2) Patients discharged to another facility
			** 3) Patients with a documented reason for no beta blockers
			** Note Marciniak et al. (1998), cited by Chandra Staiger (2007) seems to define patients meeting the above criteria as
			** "ideal" patients.  
			gen byte bbd_die_transfer_reason_exc = bbd_ideal == 1
			
			** Attempting to create a joint indicator for meeting the following criteria
			** 1) Patients who died during stay
			** 2) Patients discharged to another facility 
			** 3) Patients with terminal illness
			** or 4) Patients with a documented reason for no aspirin
			** Note Marciniak et al. (1998), cited by Chandra Staiger (2007) seems to define patients meeting the above criteria as
			** "ideal" patients. 			
			gen byte asd_die_transfer_reason_exc = asd_ideal == 1
					
		tempfile CCP_data_exclusionvars
		save `CCP_data_exclusionvars'

		** Calculates each measure for each hospital after exclusions
		
			** 1) Beta blockers at discharge
			use `CCP_data_exclusionvars', replace	
			count if los_exc | med_advice_exc | term_illness | bbd_die_transfer_reason_exc 
			local excluded = r(N)
			drop if los_exc | med_advice_exc | term_illness | bbd_die_transfer_reason_exc 
			log using CCP_logfile.log, append
			if `excluded' > 10 {
				display "count of patients after beta blocker at discharge exclusions"
				count
			}
			display "count of unique providers after beta blocker at discharge exclusions"
			codebook pn, compact
			log close

			collapse (mean) bbd, by(pn_new)
			rename bbd betablock_discharge1
			tempfile CCP_data_bbd
			save `CCP_data_bbd'
			
		
			** 2) Aspirin at discharge
			use `CCP_data_exclusionvars', replace	
			count if los_exc | med_advice_exc | asd_die_transfer_reason_exc 
			local excluded = r(N)
			drop if los_exc | med_advice_exc | asd_die_transfer_reason_exc 
			log using CCP_logfile.log, append
			if `excluded' > 10 {
				display "count of patients after aspirin at discharge exclusions"
				count
			}
			display "count of unique providers after aspirin at discharge exclusions"
			codebook pn, compact
			log close

			collapse (mean) asd, by(pn_new)
			rename asd aspirin_discharge1
			tempfile CCP_data_asd
			save `CCP_data_asd'
						
			
			** Merges together
			clear
			use `CCP_data_bbd', replace
			merge 1:1 pn_new using `CCP_data_asd', nogenerate
			tempfile CCP_data_measures
			save `CCP_data_measures'
			
		
	** STEP 2: Generate a hospital level file for 2010-2014 with outcome measures (unriskadjusted)
		
		** CMS process of care measures
		clear
		use pn ami2_* ami5_*  year using  Exportable_RawData/poc.dta, replace
		keep if year > 2009 & year < 2015
		merge m:1 pn using  Intermediate_Output_Not_Exportable/pn2pn_new.dta, keep(master match) nogenerate
		
		foreach num of numlist 1 2 5 6{
			generate ami`num'_count = ami`num'_share*ami`num'_denom
			drop ami`num'_share
		}
		
		collapse (sum) ami*, by(pn_new)
		
		generate betablock_discharge4 = ami5_count / ami5_denom
		generate aspirin_discharge4 = ami2_count / ami2_denom

		keep betablock_discharge4 aspirin_discharge4  pn_new
		tempfile CMS_data_measures
		save `CMS_data_measures'
		
		

	** STEP 3: Merges in data with market share data & creates master dataset
		
		clear
		use `CMS_data_measures', replace
		merge 1:1 pn_new using `CCP_data_measures', generate(merge_CCP_data)
		
		** Merges in patient count data
		** Makes sure that all the hospitals in the main dataset appear in at least either the CMS or CCP data
		merge 1:1 pn_new using  Exportable_IntermediateData/hospitals_market_share.dta, assert(match master) keep(match) nogenerate		
		
		** Generates flags for not being in the final sample
		gen byte only_CMS = merge_CCP_data == 1	
		gen byte only_CCP = merge_CCP_data == 2
		gen byte both_CCP_CMS = merge_CCP_data == 3
		drop merge_CCP_data
		
		foreach var in `measlist'{
		gen byte any_missing_`var' = `var'4 == . | `var'1 == . 
		}
		
		tempfile temp_widedataset
		save `temp_widedataset'
				
		** Excludes hospitals that are either 1) missing from one of the datasets or 2) have missing values in either dataset
		foreach var in `measlist'{
			
			clear
			use `temp_widedataset', replace
				
				** Limits to correct sample
				display "Dropping hospitals that do not appear in CCP data"
				count if only_CMS == 1
				drop if only_CMS == 1
			
				display "Dropping hospitals that do not appear in CMS data"
				count if only_CCP == 1
				drop if only_CCP == 1	
				
				display "Dropping hospitals with missing data"
				count if any_missing_`var'
				drop if any_missing_`var' == 1	
				
				** Limits to only relevant variables
				keep pn_new `var'4 `var'1 npat* 
			
			tempfile decomposition_data_`var'
			save `decomposition_data_`var''
			
			
		}
	
}

********************** GENERATE NEW BOODSTRAP SAMPLES ************************

if $BOOTSAMP ==1{
	
	 foreach var in `measlist'{
		
		foreach num of numlist 0/$NBOOT{
			
			** Creates bootstrap samples
			clear
			use `decomposition_data_`var'', replace
			
			if "`num'" != "0"{
				count
				local samp = r(N)
				bsample `samp'
			}			
			
			** Generates market share
				
				** Actual market share
				foreach period of numlist 1 4{
				foreach race in `racegroups'{
				
					summarize npatients_`race'_ami_tot`period'
					local tot = r(sum)
					generate mkt_share_act_`race'`period' = npatients_`race'_ami_tot`period' / `tot'
					
					** Some checks			
					assert npatients_`race'_ami_tot`period' != . 
					assert mkt_share_act_`race'`period' != . 
					summarize mkt_share_act_`race'`period'
					assert abs(1-r(sum))< .0001
				}
				}
				
				** Reweighted market shares
				foreach share in `sharetypes_reweight'{
				foreach period of numlist 1 4{
				
					summarize npatients_w_ami_tot_`share'`period'
					local tot = r(sum)
					
					generate mkt_share_`share'_w`period' = npatients_w_ami_tot_`share'`period' / `tot'
					
					** Replaces reweighted black patient marketshare with actual black patient market share. 			
					generate mkt_share_`share'_b`period' = mkt_share_act_b`period'			
					
					** Some checks
					assert mkt_share_`share'_b`period' != . 
					assert npatients_w_ami_tot_`share'`period'	!= . 
					assert mkt_share_`share'_w`period' != . 
					summarize mkt_share_`share'_w`period'
					assert abs(1-r(sum))< .0001
				}
				}
				
			** Generates a total market share (from just white / black patients
				foreach period of numlist 1 4{
					generate total_pop`period' = npatients_w_ami_tot`period' + npatients_b_ami_tot`period'
					summarize total_pop`period'
					local total_pop = r(sum)
					generate total_mkt_share_act`period' = total_pop`period' / `total_pop'
				}
							
			
			generate pn_postboot = _n 
			tempfile decomposition_data_`var'_bs`num'
			save `decomposition_data_`var'_bs`num''
			
			
		}
	}
	
}


************ AVERAGE OF POC MEASURES IN EACH PERIOD **************

if $AVERAGES == 1{
	foreach var in `measlist'{
		clear
		use `decomposition_data_`var'_bs0', replace
		foreach period of numlist 1 4{
		generate avg_period`period' = total_mkt_share_act`period'*`var'`period'
		}
		generate temp = 1
		collapse (sum) avg_period*,by(temp)
		capture noisily mkdir ExportableResults/CCP_POS/
		export excel using  ExportableResults/CCP_POS/averages_`var'.xlsx, firstrow(variables) replace		
		
	}
}

********************** STATIC DECOMPOSITION ************************

if $STATIC == 1{
	
	** Generates results including bootstrapping
	
	foreach var in `measlist'{
	foreach num of numlist 0/$NBOOT{
		
		clear
		use `decomposition_data_`var'_bs`num'', replace
		generate temp = 1
		generate hospital_sample = 1
		
		** Generates result in each period
		foreach period of numlist 1 4{
		generate between_period`period' = (mkt_share_act_b`period' - mkt_share_act_w`period')*`var'`period'					
		generate extra_period`period' = (mkt_share_act_b`period' - mkt_share_wgt_b`period')*(`var'`period')
		generate preferences_period`period' = (mkt_share_wgt_b`period' - mkt_share_wgt_w`period')*(`var'`period')
		generate micro_geography_period`period' = (mkt_share_wgt_w`period' - mkt_share_wgtH_w`period')*(`var'`period')
		generate macro_geography_period`period' = (mkt_share_wgtH_w`period' - mkt_share_act_w`period')*(`var'`period')		
		}
		** Generates difference across periods
		generate between_diff = between_period4 - 	between_period1				
		generate extra_diff = extra_period4 - extra_period1
		generate preferences_diff = preferences_period4 - preferences_period1
		generate micro_geography_diff = micro_geography_period4 - micro_geography_period1
		generate macro_geography_diff = macro_geography_period4 - macro_geography_period1
		
		
		collapse (sum) between* extra* preferences* micro_geography* macro_geography* (sum) hospital_sample, by(temp)
		foreach period of numlist 1 4{
		assert abs(between_period`period' - (extra_period`period' + preferences_period`period' + micro_geography_period`period' + macro_geography_period`period')) < .00001
		}
		tempfile static_decomp_`var'_bs`num'
		save `static_decomp_`var'_bs`num''
		
	}
	}
	
	** Exports results
	foreach var in `measlist'{
		display "`var'"
	** Exports coefficients
		clear
		use `static_decomp_`var'_bs0', replace
		drop temp
		export excel using  ExportableResults/CCP_POS/static_`var'.xlsx, firstrow(variables) replace

	
	** Generates bootstraped standard errors
		if $NBOOT > 1{
			display "bootstrapping `var'"
			use `static_decomp_`var'_bs1', replace
			foreach num of numlist 0/$NBOOT{
			append using `static_decomp_`var'_bs`num''
			}
			collapse (mean) mean_between_period1 = between_period1 mean_extra_period1 = extra_period1 mean_preferences_period1 = preferences_period1 mean_micro_geography_period1 = micro_geography_period1 mean_macro_geography_period1 = macro_geography_period1 mean_between_period4 = between_period4 mean_extra_period4 = extra_period4 mean_preferences_period4 = preferences_period4 mean_micro_geography_period4 = micro_geography_period4 mean_macro_geography_period4 = macro_geography_period4 mean_between_diff = between_diff mean_extra_diff = extra_diff mean_preferences_diff = preferences_diff mean_micro_geography_diff = micro_geography_diff mean_macro_geography_diff = macro_geography_diff (sd) sd_between_period1 = between_period1 sd_extra_period1 = extra_period1 sd_preferences_period1 = preferences_period1 sd_micro_geography_period1 = micro_geography_period1 sd_macro_geography_period1 = macro_geography_period1 sd_between_period4 = between_period4 sd_extra_period4 = extra_period4 sd_preferences_period4 = preferences_period4 sd_micro_geography_period4 = micro_geography_period4 sd_macro_geography_period4 = macro_geography_period4 sd_between_diff = between_diff sd_extra_diff = extra_diff sd_preferences_diff = preferences_diff sd_micro_geography_diff = micro_geography_diff sd_macro_geography_diff = macro_geography_diff , by(temp)
			
			local varlist "between extra preferences micro_geography macro_geography"
			local vartypes "period1 period4 diff"
			foreach value in `varlist'{
			foreach type in `vartypes'{
				generate tstat = abs(mean_`value'_`type'/sd_`value'_`type')
				generate pvalue_`value'_`type' = (1-normal(tstat))*2
				drop tstat
			}
			}
			keep sd* pvalue*
			export excel using  ExportableResults/CCP_POS/static_`var'_std_errors_and_pvalues.xlsx, firstrow(variables) replace		
		}
	}	

}


********************** DYNAMIC DECOMPOSITION ************************

if $DYNAMIC == 1{
	
	foreach var in `measlist'{
	foreach num of numlist 0/$NBOOT{
		
		clear
		use `decomposition_data_`var'_bs`num'', replace
		generate temp = 1
		generate hospital_sample = 1
		
		generate diff_in_diff = (mkt_share_act_b4*`var'4 - mkt_share_act_w4*`var'4) - (mkt_share_act_b1*`var'1 - mkt_share_act_w1*`var'1) 
		generate pure_reallocation = `var'1*((mkt_share_act_b4 - mkt_share_act_b1)-(mkt_share_act_w4 - mkt_share_act_w1))	
		generate pure_qual_imp = (mkt_share_act_b1 - mkt_share_act_w1)*(`var'4-`var'1)
		generate first_cross = ((mkt_share_act_b4 - mkt_share_act_w4)-(mkt_share_act_b1 - mkt_share_act_w1))*(`var'4-`var'1)		
		
		collapse (sum) diff_in_diff pure_reallocation pure_qual_imp first_cross (sum) hospital_sample, by(temp)
		assert abs(diff_in_diff - (pure_reallocation + pure_qual_imp + first_cross)) < .000001
		tempfile dynamic_decomp_`var'_bs`num'
		save `dynamic_decomp_`var'_bs`num''
		
	}
	}
	
	** Exports results
	foreach var in `measlist'{
		display "`var'"
	** Exports coefficients
		clear
		use `dynamic_decomp_`var'_bs0', replace	
		drop temp
		export excel using  ExportableResults/CCP_POS/dynamic_`var'.xlsx, firstrow(variables) replace

	
	** Generates bootstraped standard errors
		if $NBOOT > 1{
			display "bootstrapping `var'"
			use `dynamic_decomp_`var'_bs1', replace
			foreach num of numlist 0/$NBOOT{
			append using `dynamic_decomp_`var'_bs`num''
			}
			collapse (mean) mean_diff_in_diff = diff_in_diff mean_pure_reallocation = pure_reallocation mean_pure_qual_imp = pure_qual_imp mean_first_cross = first_cross (sd) sd_diff_in_diff = diff_in_diff sd_pure_reallocation = pure_reallocation sd_pure_qual_imp = pure_qual_imp sd_first_cross = first_cross, by(temp)
			
			local varlist "diff_in_diff pure_reallocation pure_qual_imp first_cross"
			foreach value in `varlist'{
				generate tstat = abs(mean_`value'/sd_`value')
				generate pvalue_`value' = (1-normal(tstat))*2
				drop tstat
			}
			
			keep sd* pvalue*
			export excel using  ExportableResults/CCP_POS/dynamic_`var'_std_errors_and_pvalues.xlsx, firstrow(variables) replace		
		}
	}		
}


